//
//  ChatViewController.m
//  Pink
//
//  Created by ZhongDaYunKe on 15/12/18.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "ChatViewController.h"
#import "ChatMessage.h"
@interface ChatViewController ()<XHMessageTableViewControllerDataSource,XHMessageTableViewControllerDelegate>

@property (nonatomic,strong)AVIMClient *client;
@property (nonatomic,strong)NSString *conversationId;
@property (nonatomic,strong)AVIMConversation *conversation;

@end

@implementation ChatViewController


- (instancetype)initWithConversationId:(NSString *)conversationId
{
    self = [super init];
    if (self) {
        // 配置输入框UI的样式
        self.allowsSendVoice = NO;
        self.allowsSendMultiMedia = NO;
        _conversationId = conversationId;
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBackgroundColor:[UIColor clearColor]];
    [self setBackgroundImage:[[self.view snapshotImage] imageByBlurDark]];
    self.messageSender = CurrentAVUser[@"nickName"];
    
    WEAKSELF
    [self.client openWithClientId:CurrentAVUser.objectId callback:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
             AVIMConversationQuery *query = [weakSelf.client conversationQuery];
            [query getConversationById:weakSelf.conversationId callback:^(AVIMConversation *conversation, NSError *error) {
                if (conversation) {
                    
                    _conversation = conversation;
                    
                    [conversation queryMessagesWithLimit:20 callback:^(NSArray *objects, NSError *error) {
                       
                        [weakSelf.messages addObjectsFromArray:[weakSelf convertXHWithObjects:objects]];
                        [weakSelf.messageTableView reloadData];
                        
                        [weakSelf scrollToBottomAnimated:NO];
                        
                    }];
                    
                     [conversation joinWithCallback:^(BOOL succeeded, NSError *error) {
                         if (succeeded) {
                             
                         }else{
                             
                         }
                         
                         
                     }];
                }else{
                    
                }
                
                
            }];
        }else{
            
        }
        
    }];
    
    
}

-(void)didSelectedAvatarOnMessage:(id<XHMessageModel>)message atIndexPath:(NSIndexPath *)indexPath
{
    
}


-(void)didSendText:(NSString *)text fromSender:(NSString *)sender onDate:(NSDate *)date
{
    
}



-(BOOL)shouldLoadMoreMessagesScrollToTop
{
    return YES;
}

-(void)loadMoreMessagesScrollTotop
{
    
    if (!self.loadingMoreMessage) {
        self.loadingMoreMessage = YES;
        
      
    
    }

}


- (NSMutableArray *)convertXHWithObjects:(NSArray *)objects
{
    NSMutableArray *xhmessages = [[NSMutableArray alloc] init];
    
    for (int i = 0 ; i < objects.count; i ++) {
        
        ChatMessage *chatmessage = objects[i];
        XHMessage *mesasge = [[XHMessage alloc] initWithText:chatmessage.text sender:chatmessage.userObj[@"nickName"] timestamp:[NSDate dateWithTimeIntervalSince1970:chatmessage.sendTimestamp]];
        mesasge.avatarUrl = chatmessage.userObj[@"avatar"];
        [xhmessages addObject:mesasge];

    }
    return xhmessages;
}

/**
 *  是否显示时间轴Label的回调方法
 *
 *  @param indexPath 目标消息的位置IndexPath
 *
 *  @return 根据indexPath获取消息的Model的对象，从而判断返回YES or NO来控制是否显示时间轴Label
 */
- (BOOL)shouldDisplayTimestampForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 10) {
        return YES;
    } else {
        return NO;
    }
}



/**
 *  协议回掉是否支持用户手动滚动
 *
 *  @return 返回YES or NO
 */
- (BOOL)shouldPreventScrollToBottomWhileUserScrolling {
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
